package com.one.group.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.one.group.admin.mapper.SpChargeStandardsMapper;
import com.one.group.model.dto.SpChargeStandardsAddDto;
import com.one.group.model.dto.SpChargeStandardsUpdateDto;
import com.one.group.model.entity.SpChargeStandards;
import com.one.group.model.vo.SpChargeStandardsVo;
import com.one.group.service.SpChargeStandardsService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpChargeStandardsServiceImpl extends ServiceImpl<SpChargeStandardsMapper, SpChargeStandards> implements SpChargeStandardsService {

    @Override
    public List<SpChargeStandards> findByChargeItemId(Long chargeItemId) {
        // 1. 创建查询条件
        QueryWrapper queryWrapper = QueryWrapper.create().from(SpChargeStandards.class);
        queryWrapper.eq(SpChargeStandards::getChargeitemId, chargeItemId);
        // 2. 执行查询
        return getMapper().selectListByQuery(queryWrapper);
    }

    @Override
    public List<SpChargeStandardsVo> listByQuery(String name, Long tenant_id) {
        return getMapper().mySelectListByQuery(name, tenant_id);
    }

    @Override
    public boolean myAdd(SpChargeStandardsAddDto spChargeStandardsAddDto) {
        try{
            //获取租户id
            Long tenant_id = Convert.toLong(StpUtil.getExtra("tenant_id"));
            //构建实体
            SpChargeStandards spChargeStandards = new SpChargeStandards();
            spChargeStandards.setName(spChargeStandardsAddDto.getName());
            spChargeStandards.setChargeitemId(spChargeStandardsAddDto.getChargeitemId());
            spChargeStandards.setCellId(spChargeStandardsAddDto.getCellId());
            spChargeStandards.setPrice(spChargeStandardsAddDto.getPrice());
            spChargeStandards.setTimeset(spChargeStandardsAddDto.getTimeset());
            spChargeStandards.setRemarks(spChargeStandardsAddDto.getRemarks());
            spChargeStandards.setPricestr();
            spChargeStandards.setTenantId(tenant_id);
            return save(spChargeStandards);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean myUpdateById(SpChargeStandardsUpdateDto spChargeStandardsUpdateDto) {
        try{
            //获取租户id
            Long tenant_id = Convert.toLong(StpUtil.getExtra("tenant_id"));
            //构建实体
            SpChargeStandards spChargeStandards = new SpChargeStandards();
            spChargeStandards.setId(spChargeStandardsUpdateDto.getId());
            spChargeStandards.setName(spChargeStandardsUpdateDto.getName());
            spChargeStandards.setChargeitemId(spChargeStandardsUpdateDto.getChargeitemId());
            spChargeStandards.setCellId(spChargeStandardsUpdateDto.getCellId());
            spChargeStandards.setPrice(spChargeStandardsUpdateDto.getPrice());
            spChargeStandards.setTimeset(spChargeStandardsUpdateDto.getTimeset());
            spChargeStandards.setRemarks(spChargeStandardsUpdateDto.getRemarks());
            spChargeStandards.setPricestr();
            spChargeStandards.setTenantId(tenant_id);
            return updateById(spChargeStandards);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<SpChargeStandards> queryListBychargeTypeName(String name) {
        //获取租户id
        Long tenant_id = Convert.toLong(StpUtil.getExtra("tenant_id"));
        return getMapper().queryListBychargeTypeName(name,tenant_id);
    }
}
